package com.firemoss.atonement.metadata.inspectors.bindability
{
	import org.as3commons.lang.ClassUtils;
	import org.as3commons.reflect.Accessor;
	import org.as3commons.reflect.Field;
	import org.as3commons.reflect.MetaData;
	import org.as3commons.reflect.MetaDataArgument;
	import org.as3commons.reflect.MetadataUtils;
	import org.as3commons.reflect.Type;
	import org.as3commons.reflect.Variable;

	/**
	 * Simply looks for [Bindable] tags, recording their dispatched events as
	 * given by as3comments.reflect
	 */
	public class DefaultBindabilityInspector implements IBindabilityInspector
	{
		/**
		 * Inspects a class for [Bindable] metadata, creating a list of unique event names
		 * to which an ObjectObserver must subscribe to watch changes.
		 */ 
		public function inspect( clazz : Class) : Array
		{
			var type :Type = Type.forClass( clazz );
			var accessors : Array = type.accessors
			var fields : Array = type.fields
			var result : Array = []
				
			for each ( var field : Field in fields )
			{
				for each ( var md : MetaData in field.metaData )
				{
					if ( md.name == "Bindable" )
					{
						for each ( var arg : MetaDataArgument in md.arguments )
						{
							if ( arg.key == "event" && result.indexOf( arg.value ) == -1 )
							{
								result.push( arg.value );
							}
						}
					}
				}
			}
			
			return result;
		}
	}
}